package com.os.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * @Author Matt
 * @Date 2022-01-08
 * @Desc: 异步函数类
 * 使用场景:
 * 1、发送短信
 * 2、发送邮件
 * 3、app推送
 * 4、节省运维凌晨发布任务的时间，提高效率
 *
 * @Version 1.0
 */
@Component
public class AsyncTask {

    @Async
    public Future<String> doTask1() throws Exception{
        System.out.println("开始做任务1");
        long start = System.currentTimeMillis();
        Thread.sleep(400);
        long end = System.currentTimeMillis();
        System.out.println("完成任务1，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务1完成");
    }

    @Async
    public Future<String> doTask2() throws Exception{
        System.out.println("开始做任务2");
        long start = System.currentTimeMillis();
        Thread.sleep(600);
        long end = System.currentTimeMillis();
        System.out.println("完成任务2，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务2完成");
    }

    @Async
    public Future<String> doTask3() throws Exception{
        System.out.println("开始做任务3");
        long start = System.currentTimeMillis();
        Thread.sleep(300);
        long end = System.currentTimeMillis();
        System.out.println("完成任务3，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务3完成");
    }
}
